import { PIC_URL } from './../../providers/Config';
import { Component, Injector } from '@angular/core';
import { IonicPage, NavParams } from 'ionic-angular';
import { BasePage } from "../basePage";
import { StorageService } from './../../providers/StorageService';

declare var $;
/**
 * Generated class for the PromptlyBuyPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-promptly-buy',
  templateUrl: 'promptly-buy.html',
})
export class PromptlyBuyPage extends BasePage {
  num = 1;
  toHome: boolean;
  toShop: boolean;
  shopInfoParams: any;
  service_price: any;
  nail_name: any;      /*用来保存当前商品的名称*/
  market_price: any;   /*用来保存原来的价格*/
  goods_id: any;       /*用来保存当前商品的 id*/
  service_pic: any;    /*用于保存服务的图片*/

  userinfo: any;
  countService_price: any;
  choiceWay:boolean;
  picrooturl: any;
  constructor(injector: Injector, public params: NavParams,public stc: StorageService) {
    super(injector);
    this.picrooturl = PIC_URL;
  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad PromptlyBuyPage');
    this.shopInfoParams = this.params.data;
    this.countService_price = this.service_price = this.params.data.service_price;
    this.nail_name = this.params.data.nail_name;
    this.market_price = this.params.data.market_price;
    this.goods_id = this.params.data.goods_id;
    this.service_pic = this.params.data.service_pic;

    /*获取传入过来的值*/
    console.log('接收到的信息为');
    console.log(this.shopInfoParams);
    console.log(this.service_price);
    console.log(this.nail_name);
    this.toShop = true;


    /*为 不在订单区域的位置绑定点击回退事件*/
    $('page-promptly-buy').click((e)=>{
      if(e.target.nodeName == 'PAGE-PROMPTLY-BUY'){
        console.log('回退');   
        this.viewCtrl.dismiss();     
      }
    })
  }
  /*页面进入之前  从 navCtrl.pop() 过来的函数也会执行这个函数*/
  ionViewDidEnter(){
    this.userinfo = this.stc.read('userinfo');
    if(this.userinfo == null){
      this.shopInfoParams.user_id = '';
    }else{
      this.shopInfoParams.user_id = this.userinfo.user_id;
    }
  }

  /*  跳转到确认 购买 **/
  toOrderPage() {
    if(this.shopInfoParams.user_id==''){
      this.navCtrl.push('LoginPage',{service_price:this.service_price,
        market_price:this.market_price,
        num:this.num,
        choice:this.choiceWay,
        goods_id:this.goods_id,
        service_pic:this.service_pic,
        service_name:this.nail_name})
    }else{
      if(this.toHome == true){
        this.choiceWay=true;
      }else{
        this.choiceWay=false;
      }
      this.navCtrl.push('OrderPage',
      {service_price:this.service_price,
        market_price:this.market_price,
        num:this.num,
        choice:this.choiceWay,
        goods_id:this.goods_id,
        service_pic:this.service_pic,
        service_name:this.nail_name});
    }
  }
  /* 关闭确认订单**/
  dismiss() {
    let data = { 'foo': 'bar' };
    this.viewCtrl.dismiss(data);
    // $ionicHistory.goBack();
  }
  /*  减少购买数量**/
  reduceNum() {
    if (this.num >1) {
      this.num--;
    } else {
      this.num = 1;
    }
    this.countService_price = this.num * this.service_price + '.00';
  }
  /* 增加购买数量**/
  plusNum() {
    this.num++;
    this.countService_price = this.num * this.service_price + '.00';
  }

  /** 上门服务 */
  isToHome() {
    this.toHome = true;
    this.toShop = false;
  }
  /** 到店服务 */
  isToShop() {
    this.toHome = false;
    this.toShop = true;
  }
}
